var express = require('express');
var router = express.Router();

// const socketServer = require('../server/socketServer');
const wsUtils = require('../common/socket-util');
const utils = require('../utils/util');


/* GET users listing. */
router.get('/', function (req, res, next) {
    // socketServer.getAllIfo((err, result, fields) => {
    //     res.send(result);
    // })
});

router.ws('/conectWs', (ws) => {
    console.log("连接成功！");
    ws.send(JSON.stringify({
        type: 'bind',
        ws_id:utils.uuid()
    }));
    ws.on('connection', function open() {
        console.log('connected');
        ws.send(Date.now());
    });
    ws.on('close', function close(code,reson) {
        console.log(code,reson);
        ws.close(0, 'connection out');
        console.log('disconnected');
        // 断开 or 关闭连接后请求将数据删除
        wsUtils.delWs(reson);
    });
    ws.on('message', function incoming(res) {
        const ress = JSON.parse(res);
        if (ress.type == 'bind') {
            wsUtils.bindWs(ws, ress.uid);
        }
    });
});

router.post('/sendMsg', function (req, res, next) {
    const ress = req.body;
    res.send(ress)
});

module.exports = router;